/****************************************************************************************
 * @file    GPIO.c 
 *
 * @author  HOUSHUO
 *
 * @date    2016-08-02
 *
 * @brief   General purpose input and output port source file.
 ***************************************************************************************/
#include "GPIO.h"

/****************************************************************************************
 * @func  LED_Init
 *
 * @param[in]  none
 * @return  none
 *
 * @brief   LED related GPIOs are configured as output.
 ***************************************************************************************/
void LED_Init( void )
{
	GPIO_InitTypeDef GPIO_InitStructure;

	/* GPIOF Periph clock enable */
	RCC_AHB1PeriphClockCmd( LED1_RCC_AHBPeriph | 
	                        LED2_RCC_AHBPeriph | 
	                        LED3_RCC_AHBPeriph | 
	                        LED4_RCC_AHBPeriph, 
	                        ENABLE );
	
	/* Configure PH2 PH3 PI8 PI10 in output pushpull mode */
	GPIO_InitStructure.GPIO_Pin = LED1_Pin;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	
	GPIO_Init( LED1_Port, &GPIO_InitStructure );

	GPIO_InitStructure.GPIO_Pin = LED2_Pin;
	GPIO_Init( LED2_Port, &GPIO_InitStructure );

	GPIO_InitStructure.GPIO_Pin = LED3_Pin;
	GPIO_Init( LED3_Port, &GPIO_InitStructure );

	GPIO_InitStructure.GPIO_Pin = LED4_Pin;
	GPIO_Init( LED4_Port, &GPIO_InitStructure );
}
/****************************************************************************************
 * @func  KEY_Init
 *
 * @param[in]  none
 * @return  none
 *
 * @brief  KEY related GPIOs are configured as input. 
 ***************************************************************************************/
void KEY_Init( void )
{
	GPIO_InitTypeDef GPIO_InitStructure;

	/* Periph clock enable */
	RCC_AHB1PeriphClockCmd( USER_KEY_RCC_AHBPeriph | 
	                        WAKEUP_KEY_RCC_AHBPeriph | 
							JOY_A_KEY_RCC_AHBPeriph |
							JOY_B_KEY_RCC_AHBPeriph |
							JOY_C_KEY_RCC_AHBPeriph |
							JOY_D_KEY_RCC_AHBPeriph |
							JOY_CTR_KEY_RCC_AHBPeriph, 
							ENABLE);

	/* Configure Input pushpull mode */
	GPIO_InitStructure.GPIO_Pin = USER_KEY_Pin;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	
	GPIO_Init(USER_KEY_Port, &GPIO_InitStructure);
	

	GPIO_InitStructure.GPIO_Pin = WAKEUP_KEY_Pin;
	GPIO_Init(WAKEUP_KEY_Port, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = JOY_A_KEY_Pin;
	GPIO_Init(JOY_A_KEY_Port, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Pin = JOY_B_KEY_Pin;
	GPIO_Init(JOY_B_KEY_Port, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Pin = JOY_C_KEY_Pin;
	GPIO_Init(JOY_C_KEY_Port, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Pin = JOY_D_KEY_Pin;
	GPIO_Init(JOY_D_KEY_Port, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Pin = JOY_CTR_KEY_Pin;
	GPIO_Init(JOY_CTR_KEY_Port, &GPIO_InitStructure);
}

/****************************************************************************************
 * @func  Read_KEY_State
 *
 * @param[in]  GPIOx: where x can be (A..I) to select the GPIO peripheral.
 *             GPIO_Pin: specifies the port bit to read.
 * @return  key_state: return the state of the specified port bit.
 *
 * @brief   Read the status of KEY GPIOs (²»´øÑÓÊ±Ïû¶¶).
 ***************************************************************************************/
uint8_t Read_KEY_State( void )
{
	if(!GPIO_ReadInputDataBit(USER_KEY_Port,USER_KEY_Pin))
		return 0;
	else if(!GPIO_ReadInputDataBit(WAKEUP_KEY_Port,WAKEUP_KEY_Pin))
		return 1;
	else if(!GPIO_ReadInputDataBit(JOY_A_KEY_Port,JOY_A_KEY_Pin))
		return 2;
	else if(!GPIO_ReadInputDataBit(JOY_B_KEY_Port,JOY_B_KEY_Pin))
		return 3;
	else if(!GPIO_ReadInputDataBit(JOY_C_KEY_Port,JOY_C_KEY_Pin))
		return 4;
	else if(!GPIO_ReadInputDataBit(JOY_D_KEY_Port,JOY_D_KEY_Pin))
		return 5;
	else if(!GPIO_ReadInputDataBit(JOY_CTR_KEY_Port,JOY_CTR_KEY_Pin))
		return 6;
	else 
		return 0xFF;/* No key is pushed */
}

/****************************************************************************************
 * @func  Led_Toggle
 *
 * @param[in]  GPIOx: where x can be (A..I) to select the GPIO peripheral.
 *             GPIO_Pin: specifies the port bit to read.
 * @return  none
 *
 * @brief   Toggle the state of LEDs.
 ***************************************************************************************/
void Led_Toggle( GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin )
{
  GPIO_ToggleBits( GPIOx, GPIO_Pin );
}
/****************************************************************************************
* @Function  : PS_Init
*
* @Breif     : Initialize PS device pins.
*
* @Param[in] : None
*
* @Return    : None
****************************************************************************************/
void PS_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;

	/* GPIOF Periph clock enable */
	RCC_AHB1PeriphClockCmd( PS_ATT_RCC_AHBPeriph | 
	                        PS_CLK_RCC_AHBPeriph | 
	                        PS_CMD_RCC_AHBPeriph | 
	                        PS_DAT_RCC_AHBPeriph, 
	                        ENABLE );
	/* Configure PI4 PI5 PI6 in output pushpull mode */
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

    GPIO_InitStructure.GPIO_Pin = PS_ATT_Pin;
	GPIO_Init(PS_ATT_Port, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = PS_CLK_Pin;
	GPIO_Init(PS_CLK_Port, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = PS_CMD_Pin;
	GPIO_Init(PS_CMD_Port, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = PS_DAT_Pin;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
	GPIO_Init(PS_DAT_Port, &GPIO_InitStructure);
}

